#include "DeltaGlider.h"
// --------------------------------------------------------------
// Respond to panel mouse event
// --------------------------------------------------------------
bool DeltaGlider::clbkPanelMouseEvent (int id, int event, int mx, int my)
{
	int mfd;
	static int ctrl = 0, mode = 0;
	//char Name[20];
	totalcrew  =  Crew.GetCrewTotalNumber();
	totalcargo = oapiGetVesselCount();
	char * Name = Crew.GetCrewNameBySlotNumber(SelectedUmmuMember);
	switch (id) {
	
	// panel 0 events: --------------------------------------------------------------------------------

    


case AID_ATTITUDEMODE:
		if (event & PANEL_MOUSE_LBDOWN) return DecAttMode();
		if (event & PANEL_MOUSE_RBDOWN) return IncAttMode();
		return false;
	case AID_ADCTRLMODE:
		if (event & PANEL_MOUSE_LBDOWN) return DecADCMode();
		if (event & PANEL_MOUSE_RBDOWN) return IncADCMode();
		return false;
	case AID_NAVMODE: {
		static int navmode[8] = {1,0,3,4,5,6,2,7};
		int mode = navmode[(mx/39)*2 + my/39];
		if (mode) ToggleNavmode (mode);
		} return true;
	case AID_HUDMODE:
		if (mx%29 < 20) oapiSetHUDMode (HUD_NONE+(mx/29));
		return true;
	case AID_ENGINEMAIN:
		
		if (event & PANEL_MOUSE_LBDOWN) { // record which slider to operate
			if      (mx <  12) ctrl = 0; // left engine
			else if (mx >= 37) ctrl = 1; // right engine
			else               ctrl = 2; // both
		}
		if ((my -= 9) < 0) my = 0;
		else if (my > 157) my = 157;
		SetMainRetroLevel (ctrl, my <= 108 ? 1.0-my/108.0  : 0.0,   // main thruster level
			                     my >= 125 ? (my-125)/32.0 : 0.0);  // retro thruster level
		return true;
	case AID_ENGINEHOVER:
		
		my -= 9;
		if      (my <   0) my =   0;
		else if (my > 116) my = 116;
		SetThrusterGroupLevel (thg_hover, 1.0-my/116.0);
		return true;
	case AID_ENGINESCRAM:
		
		if (event & PANEL_MOUSE_LBDOWN) { // record which slider to operate
			if      (mx <  12) ctrl = 0; // left engine
			else if (mx >= 37) ctrl = 1; // right engine
			else               ctrl = 2; // both
		}
		SetScramLevel (ctrl, max (0.0, min (1.0, 1.0-my/84.0)));
		return true;
	case AID_HOVERBALANCE:
		
		if (event & PANEL_MOUSE_LBDOWN) {
			if (my < 22) mode = 1;
			else         mode = 2;
		} else if (event & PANEL_MOUSE_LBUP) {
			mode = 0;
		}
		if (ShiftHoverBalance (mode))
			oapiTriggerPanelRedrawArea (0, AID_HBALANCEDISP);
		return (event & PANEL_MOUSE_LBDOWN || event & PANEL_MOUSE_LBUP);
	case AID_HBALANCEMODE:
		
		hbmode = 1-hbmode;
		return true;
	case AID_PGIMBALMAIN:
		
		if (event & PANEL_MOUSE_LBDOWN) {
			if      (mx <  10) ctrl = 1;
			else if (mx >= 25) ctrl = 2;
			else               ctrl = 3;
			if      (my <  22) mode = 1;
			else               mode = 2;
		} else if (event & PANEL_MOUSE_LBUP) {
			ctrl = 0;
		}
		if (IncMainPGimbal (ctrl, mode))
			oapiTriggerPanelRedrawArea (0, AID_PGIMBALMAINDISP);
		return (event & PANEL_MOUSE_LBDOWN || event & PANEL_MOUSE_LBUP);
	case AID_PGIMBALMAINMODE:
		
		mpmode = 1-mpmode;
		return true;
	case AID_YGIMBALMAINMODE:
		
		mode = mymode;
		if (my < 12)                  mymode = (mode == 1 ? 0 : 1);
		else if (my >= 17 && my < 29) mymode = (mode == 2 ? 0 : 2);
		else if (my >= 34)            mymode = (mode == 3 ? 0 : 3);
		return (mode != mymode);
	case AID_GIMBALSCRAMMODE:
		
		spmode = 1-spmode;
		return true;
	case AID_YGIMBALMAIN:
		
		if (event & PANEL_MOUSE_LBDOWN) {
			if      (my <  10) ctrl = 1;
			else if (my >= 25) ctrl = 2;
			else               ctrl = 3;
			if      (mx <  22) mode = 1;
			else               mode = 2;
		} else if (event & PANEL_MOUSE_LBUP) {
			ctrl = 0;
		}
		if (IncMainYGimbal (ctrl, mode))
			oapiTriggerPanelRedrawArea (0, AID_YGIMBALMAINDISP);
		return (event & PANEL_MOUSE_LBDOWN || event & PANEL_MOUSE_LBUP);
	case AID_GIMBALSCRAM:
		
		if (event & PANEL_MOUSE_LBDOWN) {
			if      (mx <  10) ctrl = 1;
			else if (mx >= 25) ctrl = 2;
			else               ctrl = 3;
			if      (my <  22) mode = 1;
			else               mode = 2;
		} else if (event & PANEL_MOUSE_LBUP) {
			ctrl = 0;
		}
		if (IncScramGimbal (ctrl, mode))
			oapiTriggerPanelRedrawArea (0, AID_GIMBALSCRAMDISP);
		return (event & PANEL_MOUSE_LBDOWN || event & PANEL_MOUSE_LBUP);
	case AID_ELEVATORTRIM:
		
		SetControlSurfaceLevel (AIRCTRL_ELEVATORTRIM,
			GetControlSurfaceLevel (AIRCTRL_ELEVATORTRIM) + 
			oapiGetSimStep() * (my < 22 ? -0.2:0.2));
		return true;
	case AID_RADIATORSWITCH:
		
		ActivateRadiator (my < 19 ? DOOR_CLOSING : DOOR_OPENING);
		break;
	case AID_HATCHSWITCH:
		
		ActivateHatch (my < 19 ? DOOR_CLOSING : DOOR_OPENING);
		break;
	case AID_LADDERSWITCH:
		
		ActivateLadder (my < 19 ? DOOR_CLOSING : DOOR_OPENING);
		break;
	case AID_MWS:
		bMWSActive = bMWSOn = true;
		return true;
	case AID_MFD1_BBUTTONS:
	case AID_MFD2_BBUTTONS:
	
		mfd = (id == AID_MFD1_BBUTTONS ? MFD_LEFT : MFD_RIGHT);
		if (mx < 26)                    oapiToggleMFD_on (mfd);
		else if (mx >= 214 && mx < 240) oapiSendMFDKey (mfd, OAPI_KEY_F1);
		else if (mx > 244)              oapiSendMFDKey (mfd, OAPI_KEY_GRAVE);
		return true;
	case AID_MFD1_LBUTTONS:
	case AID_MFD1_RBUTTONS:
	case AID_MFD2_LBUTTONS:
	case AID_MFD2_RBUTTONS:
		if (my%41 < 18) {
			mfd = (id <= AID_MFD1_RBUTTONS ? MFD_LEFT : MFD_RIGHT);
			int bt = my/41 + (id == AID_MFD1_LBUTTONS || id == AID_MFD2_LBUTTONS ? 0 : 6);
			oapiProcessMFDButton (mfd, bt, event);
			return true;
		}
		break;

	case AID_Environmental_Control:
          
		
		
		if ((mx < 5) && (my > 66)) 
		{
			  Environmental_Control_Computer_page -- ;
			  if (Environmental_Control_Computer_page == -1) Environmental_Control_Computer_page = Environmental_Control_Computer_page_Count -1;
			
			break;
			}
   
        if ((mx > 8) && (mx < 15) && (my > 66))
		{
			Environmental_Control_Computer_page ++ ;
		         if (Environmental_Control_Computer_page == Environmental_Control_Computer_page_Count  ) Environmental_Control_Computer_page = 0;
			break;
		    }


       if (Environmental_Control_Computer_page == 1)
	   {
           //Cabin Thermostat Set
	       if ((mx > 85) && (mx <  95) && (my > 21) && (my < 31))   CabinThermostatSet -- ;
	       if ((mx > 95) && (mx < 105) && (my > 21) && (my < 31))  CabinThermostatSet ++ ;
		   // cooling loop
	       if ((mx > 85) && (mx <  95) && (my > 33) && (my < 43))
		      {
			   Radiator_loop -- ;
		       if (Radiator_loop == 0) Radiator_loop = 4;
		      }
	       if ((mx > 95) && (mx < 105) && (my > 33) && (my < 43))
		       {
                Radiator_loop ++ ;
		        if (Radiator_loop == 5) Radiator_loop = 1;
			   }
	       if ((mx > 85) && (mx < 95) && (my > 43) && (my < 53)) 
		      {
		      CabinThermostat_battery_loop --;
		      if (CabinThermostat_battery_loop == 0) CabinThermostat_battery_loop = 4;
		      }
           if ((mx > 95) && (mx < 105) && (my > 43) && (my < 53)) 
		      {
              CabinThermostat_battery_loop ++;
		      if (CabinThermostat_battery_loop == 5) CabinThermostat_battery_loop = 1;

		      }
	   
	   
	   }
		
		//if ((mx > 00) && (mx < 00) && (my > 00) && (my < 00)) 
		
		
		
		
		
break;
















	case AID_EVA_Selector:
		if (totalcrew > 0){	
	 
	if  (my < 15) 
		 {
			 
			 if (SelectedUmmuMember == totalcrew -1  )
			 {
				 SelectedUmmuMember = 0;
				 break;
			 }						
			 SelectedUmmuMember = SelectedUmmuMember + 1;
	         
	        }
	else  
		 {			
			 if (SelectedUmmuMember == 0){				 
				 SelectedUmmuMember = totalcrew -1  ;
				 break;
			 }
			 SelectedUmmuMember = SelectedUmmuMember - 1;
			
	         }		
		
		break;	
		}
	case AID_Cargo_Selector:
	
		
	if  (my < 15) 
		 {
						 
				 
						
			 SelectedCrgo = SelectedCrgo + 1;
	         cargoUpDown = 1;
	}
	else  
	{
			 SelectedCrgo = SelectedCrgo - 1;
			 

			
	         cargoUpDown = 0;
			
		
		}		
		
		break;		
		
		
// deploy person for now
		case AID_Enter_Airlock:
			if (OUTER_AIRLOCK_DOOR_STATUS == DOOR_OPEN ) 
			{
				Crew.EvaCrewMember(Name);
			}
		break;		



		


	// panel 1 events: --------------------------------------------------------------------------------

	case AID_OUTER_AIRLOCK_DOOR:
		if (NOSE_CONE_STATUS == DOOR_OPEN  && POWER == SYSTEM_ON)
		{

		if (my < 39) 
		{
		ActivateOuterAirlock (DOOR_CLOSING);
		
		}
		else
		{
        ActivateOuterAirlock (DOOR_OPENING);
		}
		
		}
		break;
	
	
	
	
	
	case AID_INNER_AIRLOCK_DOOR:
	
		ActivateInnerAirlock (my < 39 ? DOOR_CLOSING : DOOR_OPENING);
		break;
	
	
	
	case AID_EXTEND_AIRLOCK:
	
		ExtendAirLock (my < 39 ? DOOR_CLOSING : DOOR_OPENING);
		break;	
		
		
		
		case AID_CHAMBER:
		
		if (my < 39) 
		{
		CHAMBER_STATUS = DOOR_CLOSED;
		FAN_AIR_LOOP_001 = SYSTEM_OFF;
		}
		else
		{
        CHAMBER_STATUS = DOOR_OPEN;
		FAN_AIR_LOOP_001 = SYSTEM_ON;
		}
				
		break;
		
		
		
		
		
		
		//--------------------------------------------------------------------TURBINE LEFT START	
	 case AID_TURBINE_FUEL_LEFT:
		 		 
		 if  ((my < 39) && (POWER == SYSTEM_ON))
		 {
			TURBINE_FUEL_LEFT_POWER = SYSTEM_OFF;
            TURBINE_START_LEFT_POWER = SYSTEM_OFF;
			//engine_activateJET(0, 0);
			

		 }
		if  (my > 39) 
		 {
			TURBINE_FUEL_LEFT_POWER = SYSTEM_ON;
						
		}		
		 
        break;	
		
        case AID_TURBINE_IGNITION_LEFT:
			
		 
			if  ((my < 39) && (POWER == SYSTEM_ON))
			{
			TURBINE_IGNITION_LEFT_POWER = SYSTEM_OFF;
		    TURBINE_RUNNING_LEFT = SYSTEM_OFF;	 
			TURBINE_START_LEFT_POWER = SYSTEM_OFF;
			}
			if  (my > 39) 
			 {
			TURBINE_IGNITION_LEFT_POWER = SYSTEM_ON;
			}		
		 
        break;	

		case AID_TURBINE_START_LEFT:
			
		     
			 if  ((my > 0) && (POWER == SYSTEM_ON) && (TURBINE_IGNITION_LEFT_POWER == SYSTEM_ON) && (TURBINE_RUNNING_LEFT == SYSTEM_OFF))
			 {
			  TURBINE_RUNNING_LEFT = SYSTEM_OFF;
				 TURBINE_START_LEFT_POWER = SYSTEM_ON;
				 //engine_activateJET(1, 0);
                
			 }
			
		 
        break;	
//--------------------------------------------------------------------TURBINE LEFT END
	
//--------------------------------------------------------------------TURBINE  RIGHT start	
	 case AID_TURBINE_FUEL_RIGHT:
		 
		 
		 if  ((my < 39) && (POWER == SYSTEM_ON))
		 {
			
			TURBINE_FUEL_RIGHT_POWER = SYSTEM_OFF;
			//engine_activateJET(0, 1);
			
		 
		 }
		if  (my > 39) 
		 {
			TURBINE_FUEL_RIGHT_POWER = SYSTEM_ON;
			
		}		
		 
        break;	
		
        case AID_TURBINE_IGNITION_RIGHT:
		
		 
			if  ((my < 39) && (POWER == SYSTEM_ON))
			{
			TURBINE_IGNITION_RIGHT_POWER = SYSTEM_OFF;
		
			 }
			if  (my > 39) 
			 {
			TURBINE_IGNITION_RIGHT_POWER = SYSTEM_ON;
			}		
		 
        break;	

		case AID_TURBINE_START_RIGHT:
			
		 
			 if  ((my > 0) && (POWER == SYSTEM_ON) && (TURBINE_IGNITION_RIGHT_POWER == SYSTEM_ON))
			 {
				 
				 TURBINE_START_RIGHT_POWER = SYSTEM_ON;
				// engine_activateJET(1, 1);

			 }
			
		 
        break;	
//--------------------------------------------------------------------TURBINE RIGHT END		
		
		
	case AID_PARKING_BRAKE:
		
		 
			if  ((my < 39) && (POWER == SYSTEM_ON))
			{
			PARKING_BRAKE = SYSTEM_OFF;
		 
			 }
			if  (my > 39) 
			 {
			PARKING_BRAKE = SYSTEM_ON;
			}		
		 
        break;		
		
		case AID_TURBINE_REVERSER:
		 
		 if  ((my < 39) && (POWER == SYSTEM_ON))
		 {
			
		 ActivateReverser (DOOR_CLOSED);
		 }
		if  ((my > 39) && (POWER == SYSTEM_ON))
		 {
			
		ActivateReverser (DOOR_OPEN);
		}		
		 
        break;		
		
		case AID_NOSECONE:
			
		ActivateNoseCone (my < 39 ? DOOR_CLOSING : DOOR_OPENING);
		break;

		

       case AID_BAY_DOOR_FORWARD_RIGHT:
		   
		   if(BAY_DOOR_FORWARD_RIGHT_POWER) Activate_bay_door_forward_right (my < 39 ? DOOR_CLOSING : DOOR_OPENING);
		break;
	
		case AID_BAY_DOOR_FORWARD_LEFT:
			
		   if(BAY_DOOR_FORWARD_LEFT_POWER) Activate_bay_door_forward_left (my < 39 ? DOOR_CLOSING : DOOR_OPENING);
		break;
		
		case AID_BAY_DOOR_AFT_RIGHT:
			
		   if(BAY_DOOR_AFT_RIGHT_POWER) Activate_bay_door_aft_right (my < 39 ? DOOR_CLOSING : DOOR_OPENING);
		break;
	
		case AID_BAY_DOOR_AFT_LEFT:
			
		    if(BAY_DOOR_AFT_LEFT_POWER) Activate_bay_door_aft_left (my < 39 ? DOOR_CLOSING : DOOR_OPENING);
		break;
		
		
		//LIGHTS
		case AID_BEACON_SWITCH:
			
		
			if  ((my < 39) && (POWER == SYSTEM_ON))
		 {
			
             BEACON_LIGHT_POWER = SYSTEM_OFF;		 
		}
		if  ((my > 39) && (POWER == SYSTEM_ON))
		 {
			
			BEACON_LIGHT_POWER = SYSTEM_ON;
		}		
		 
        break;		
// nav
		case AID_NAVIGATION_SWITCH:
			
		
			if  ((my < 39) && (POWER == SYSTEM_ON))
		 {
			
             NAVIGATION_LIGHTS_POWER = SYSTEM_OFF;		 
		}
		if  ((my > 39) && (POWER == SYSTEM_ON))
		 {
			
			NAVIGATION_LIGHTS_POWER = SYSTEM_ON;
		}		
		break;		
		
		// strobe
		case AID_STROBE_SWITCH:
			
		
			if  ((my < 39) && (POWER == SYSTEM_ON))
		 {
			
             STROBE_LIGHTS_POWER = SYSTEM_OFF;		 
		}
		if  ((my > 39) && (POWER == SYSTEM_ON))
		 {
			
			STROBE_LIGHTS_POWER = SYSTEM_ON;
		}		
		break;		

		// panel 2 events:
	case AID_WBRAKE_BOTH:
		
	case AID_WBRAKE_LEFT:
		
	case AID_WBRAKE_RIGHT:
		
		my -= 43;
		if      (my <  0) my =  0;
		else if (my > 93) my = 93;
		SetWheelbrakeLevel (my/93.0, id-AID_WBRAKE_BOTH, true);
		return true;
	
	
	case AID_DOCKRELEASE:
		
		if (event == PANEL_MOUSE_LBDOWN) Undock (0);
		dockreleasedown = (event == PANEL_MOUSE_LBDOWN);
		return true;
	    break;






   // over head power panel
case AID_MASTER_POWER:
	
		 if  (my < 39) 
		 {
			
			POWER = SYSTEM_OFF;
		    TURBINE_FUEL_LEFT_POWER = SYSTEM_OFF;
			TURBINE_IGNITION_LEFT_POWER = SYSTEM_OFF;
		    TURBINE_RUNNING_LEFT = SYSTEM_OFF;
		    PARKING_BRAKE = SYSTEM_OFF;
			MASTER_BAY_DOORS_POWER = SYSTEM_OFF;
		    BAY_DOOR_FORWARD_RIGHT_POWER = SYSTEM_OFF;
			BAY_DOOR_FORWARD_LEFT_POWER = SYSTEM_OFF;
            BAY_DOOR_AFT_RIGHT_POWER = SYSTEM_OFF;
		    BAY_DOOR_AFT_LEFT_POWER = SYSTEM_OFF;
		 }
		if  (my > 39) 
		 {
			
		   POWER = SYSTEM_ON;
		 }		
		
		break;	

		case AID_MASTER_BAY_DOORS_POWER:
			
		 if(POWER == SYSTEM_ON)
		 {		
			 if  (my < 39) 
		 {
			MASTER_BAY_DOORS_POWER = SYSTEM_OFF;
			BAY_DOOR_FORWARD_RIGHT_POWER = SYSTEM_OFF;
		    BAY_DOOR_FORWARD_LEFT_POWER = SYSTEM_OFF;
		    BAY_DOOR_AFT_RIGHT_POWER = SYSTEM_OFF;
		    BAY_DOOR_AFT_LEFT_POWER = SYSTEM_OFF;
			 }
		if  (my > 39) 
		 {
			
		   MASTER_BAY_DOORS_POWER = SYSTEM_ON;
		 }		
		 }
		break;	

	case AID_BAY_DOOR_FORWARD_RIGHT_POWER:
		
		 if(MASTER_BAY_DOORS_POWER == SYSTEM_ON)
		 {		
			 if  (my < 39) 
		 {
			BAY_DOOR_FORWARD_RIGHT_POWER = SYSTEM_OFF;
			
		 }
		if  (my > 39) 
		 {
			
		   BAY_DOOR_FORWARD_RIGHT_POWER = SYSTEM_ON;
		 }		
		 }
		break;	

	case AID_BAY_DOOR_FORWARD_LEFT_POWER:
		
		 if(MASTER_BAY_DOORS_POWER == SYSTEM_ON)
		 {		
			 if  (my < 39) 
		 {
			BAY_DOOR_FORWARD_LEFT_POWER = SYSTEM_OFF;
			
		 }
		if  (my > 39) 
		 {
			
		   BAY_DOOR_FORWARD_LEFT_POWER = SYSTEM_ON;
		 }		
		 }
		break;	

	case AID_BAY_DOOR_AFT_RIGHT_POWER:
		
		 if(MASTER_BAY_DOORS_POWER == SYSTEM_ON)
		 {		
			 if  (my < 39) 
		 {
			BAY_DOOR_AFT_RIGHT_POWER = SYSTEM_OFF;
			
		 }
		if  (my > 39) 
		 {
			
		   BAY_DOOR_AFT_RIGHT_POWER = SYSTEM_ON;
		 }		
		 }
		break;	

		case AID_BAY_DOOR_AFT_LEFT_POWER:
			
		 if(MASTER_BAY_DOORS_POWER == SYSTEM_ON)
		 {		
			 if  (my < 39) 
		 {
			BAY_DOOR_AFT_LEFT_POWER = SYSTEM_OFF;
			
		 }
		if  (my > 39) 
		 {
			
		   BAY_DOOR_AFT_LEFT_POWER = SYSTEM_ON;
		 }		
		 }
		break;	

	case AID_AIR_LOCK_SYSTEM_POWER:
		if (POWER == SYSTEM_ON)
		{

		if (my < 39)
		{
		AIRLOCK_MASTER_POWER = SYSTEM_OFF;}
		else
		{
        AIRLOCK_MASTER_POWER = SYSTEM_ON;}
		}
		break;

	case AID_NOSECONE_POWER:
		if (POWER == SYSTEM_ON)
		{

		if (my < 39)
		{
		NOSE_CONE_POWER = SYSTEM_OFF;}
		else
		{
        NOSE_CONE_POWER = SYSTEM_ON;}
		}
		break;

	case AID_EXTEND_AIRLOCK_POWER:
		if (POWER == SYSTEM_ON)
		{

		if (my < 39)
		{
		AIRLOCK_TUBE_POWER = SYSTEM_OFF;}
		else
		{
        AIRLOCK_TUBE_POWER = SYSTEM_ON;}
		}
		break;

	case AID_CHAMBER_POWER:
		if (POWER == SYSTEM_ON)
		{

		if (my < 39)
		{
		CHAMBER_POWER = SYSTEM_OFF;
		
		}
		else
		{
        CHAMBER_POWER = SYSTEM_ON;}
		
		}
		break;

	case AID_INNER_AIRLOCK_DOOR_POWER:
		if (POWER == SYSTEM_ON)
		{

		if (my < 39)
		{
		INNER_AIR_LOCK_DOOR_POWER = SYSTEM_OFF;}
		else
		{
        INNER_AIR_LOCK_DOOR_POWER = SYSTEM_ON;}
		}
		break;

	case AID_OUTER_AIRLOCK_DOOR_POWER:
		if (POWER == SYSTEM_ON)
		{

		if (my < 39)
		{
		OUTER_AIR_LOCK_DOOR_POWER = SYSTEM_OFF;}
		else
		{
        OUTER_AIR_LOCK_DOOR_POWER = SYSTEM_ON;}
		}
		break;

	case AID_DOCK_RELEASE_POWER:
		if (POWER == SYSTEM_ON)
		{

		if (my < 39)
		{
		DOCK_RELEASE_POWER = SYSTEM_OFF;}
		else
		{
        DOCK_RELEASE_POWER = SYSTEM_ON;}
		}
		break;





}
	return false;
}
